#include "cadrerewardtab.h"
#include "ui_cadrerewardtab.h"
#include <QDialog>
#include <QGridLayout>
#include <QLineEdit>
#include <QSqlQuery>
#include <QPushButton>
#include <QSqlTableModel>
#include "timeeditdelegate.h"
#include <QDebug>
#include <QLabel>
#include <QDialogButtonBox>
#include <QMessageBox>
#include <QSqlError>
#include "cdateedit.h"
#include "cadreutil.h"

CadreRewardTab::CadreRewardTab(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::CadreRewardTab)
{
    ui->setupUi(this);
}

CadreRewardTab::~CadreRewardTab()
{
    delete ui;
}
void CadreRewardTab::initRewardTableView(QString curPerson)
{
    rewardModel = new QSqlTableModel(this);
    rewardModel->setTable("Reward");
    rewardModel->setEditStrategy(QSqlTableModel::OnFieldChange);// OnRowChange OnFieldChange OnManualSubmit
    QString filter = QString::fromUtf8("IDNum='%1'").arg(curPerson);
    currentPerson=curPerson;
    rewardModel->setFilter(filter);
    rewardModel->select();

    rewardModel->setHeaderData(1, Qt::Horizontal, QString::fromUtf8("奖励时间"));
    rewardModel->setHeaderData(2, Qt::Horizontal, QString::fromUtf8("奖励类型"));
    rewardModel->setHeaderData(3, Qt::Horizontal, QString::fromUtf8("人员受奖励名称"));
    rewardModel->setHeaderData(4, Qt::Horizontal, QString::fromUtf8("等级"));
    rewardModel->setHeaderData(5, Qt::Horizontal, QString::fromUtf8("排名"));
    ui->rewardTableView->horizontalHeader()->setVisible(true); //UI界面设置，总是恢复为false
    ui->rewardTableView->setModel(rewardModel);
    TimeEditDelegate *rewardtime = new TimeEditDelegate();
    ui->rewardTableView->setItemDelegateForColumn(1,rewardtime);
    ui->rewardTableView->setColumnHidden(0,true);
    ui->rewardTableView->setColumnHidden(6,true);
    ui->rewardTableView->setColumnWidth(1,130);
    ui->rewardTableView->setColumnWidth(2,130);
    ui->rewardTableView->setColumnWidth(3,180);
    ui->rewardTableView->setColumnWidth(4,130);
    ui->rewardTableView->setColumnWidth(5,150);
    ui->rewardTableView->selectRow(rewardModel->rowCount()-1);
}
void CadreRewardTab::setButtonEnable(bool i)
{
    ui->rewardAddButton->setEnabled(i);
    ui->rewardDeleteButton->setEnabled(i);
    ui->rewardDownButton->setEnabled(i);
    ui->rewardUpButton->setEnabled(i);
    if(i){
        ui->rewardTableView->setEditTriggers(QAbstractItemView::DoubleClicked);
    }else{
        ui->rewardTableView->setEditTriggers(QAbstractItemView::NoEditTriggers);
    }
}

void CadreRewardTab::on_rewardAddButton_clicked()
{
    QDialog *addDialog = new QDialog(this);
    addDialog->setWindowTitle(trUtf8("添加奖励"));
    addDialog->setGeometry(700,300,400,380);
    addDialog->setContentsMargins(10,0,10,0);
    QGridLayout *gridLayout = new QGridLayout(addDialog);
    gridLayout->setMargin(10);
    QLabel *timeLabel =new QLabel(trUtf8("奖励时间"),addDialog);
    QLabel *typeLabel=new QLabel(trUtf8("奖励类型"),addDialog);
    QLabel *rewardnameLabel=new QLabel(trUtf8("人员受奖励名称"),addDialog);
    QLabel *levelLabel=new QLabel(trUtf8("等级"),addDialog);
    QLabel *rankingLabel=new QLabel(trUtf8("排名"),addDialog);

    QLineEdit *type = new QLineEdit(addDialog);
    QLineEdit *rewardname = new QLineEdit(addDialog);
    QLineEdit *level = new QLineEdit(addDialog);
    CDateEdit *time = new CDateEdit(addDialog);
    time->setNullable(true);
    time->setDisplayFormat("yyyy-MM-dd");
    time->setCalendarPopup(true);
    time->setDate(QDate());
    QLineEdit *ranking = new QLineEdit(addDialog);

    gridLayout->addWidget(timeLabel,0,0);
    gridLayout->addWidget(time,0,1);
    gridLayout->addWidget(typeLabel,1,0);
    gridLayout->addWidget(type,1,1);
    gridLayout->addWidget(rewardnameLabel,2,0);
    gridLayout->addWidget(rewardname,2,1);
    gridLayout->addWidget(levelLabel,3,0);
    gridLayout->addWidget(level,3,1);
    gridLayout->addWidget(rankingLabel,4,0);
    gridLayout->addWidget(ranking,4,1);

    QPushButton *yesButton = new QPushButton(trUtf8("确定"));
    QPushButton *cancelButton = new QPushButton(trUtf8("取消"));

    QDialogButtonBox *buttonBox = new QDialogButtonBox(Qt::Horizontal);
    buttonBox->addButton(yesButton, QDialogButtonBox::AcceptRole);
    buttonBox->addButton(cancelButton, QDialogButtonBox::RejectRole);

    gridLayout->addWidget(buttonBox,5,0,1,2,Qt::AlignHCenter);

    connect(buttonBox, SIGNAL(accepted()), addDialog, SLOT(accept()));
    connect(buttonBox, SIGNAL(rejected()), addDialog, SLOT(reject()));

    if(addDialog->exec()==QDialog::Accepted){
        QSqlQuery query;
        //    SELECT `RewardNum`, `RewardTime`, `RewardType`, `RewardName`, `RewardLevel`,
        //            `RewardRanking`, `IDNum`
        //    FROM zzb.reward;

        query.prepare("insert into Reward (`RewardTime`, `RewardType`,"
                      " `RewardName`, `RewardLevel`, `RewardRanking`, `IDNum`)"
                      " values(:time, :type, :rewardname, :level, :ranking, "
                      ":idnum )");
        query.bindValue(":time",time->date());
        query.bindValue(":type",type->text());
        query.bindValue(":rewardname",rewardname->text());
        query.bindValue(":level",level->text());
        query.bindValue(":ranking",ranking->text());
        query.bindValue(":idnum",currentPerson);
        if(query.exec()){
            QMessageBox::information(this,trUtf8("提示"),
               trUtf8("插入数据成功"),QMessageBox::Yes);
            initRewardTableView(currentPerson);
        }else{
            QString info = query.lastError().databaseText();
            QMessageBox::information(this,trUtf8("插入数据库错误"),
               info,QMessageBox::Yes);
        }
    }
}

void CadreRewardTab::on_rewardDeleteButton_clicked()
{
    CadreUtil::deleteRow(rewardModel, ui->rewardTableView);
}

void CadreRewardTab::on_rewardUpButton_clicked()
{
    int cur = CadreUtil::moveUpOne(rewardModel,0,ui->rewardTableView);
    initRewardTableView(currentPerson);
    ui->rewardTableView->selectRow(cur);
}

void CadreRewardTab::on_rewardDownButton_clicked()
{
    int cur = CadreUtil::moveDownOne(rewardModel,0,ui->rewardTableView);
    initRewardTableView(currentPerson);
    ui->rewardTableView->selectRow(cur);
}
